package org.go.examples.example_05;

import java.util.Calendar;
import java.util.Date;

import org.go.domain.TriggerType;
import org.go.examples.example_01.MyTask;
import org.go.scheduler.Scheduler;
import org.go.scheduler.SchedulerFactory;
import org.go.scheduler.StdSchedulerFactory;
import org.go.trigger.Trigger;
import org.go.trigger.TriggerBuilder;
import org.go.work.Work;
import org.go.work.WorkBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This Example will demonstrate how to start and shutdown the Quartz 
 * scheduler and how to schedule a work to run in Go.
 * 
 * @author hejie
 */
public class SimpleExample {

	public void run() throws Exception {
		Logger log = LoggerFactory.getLogger(SimpleExample.class);

		log.info("------- Initializing ----------------------");

		// First we must get a reference to a scheduler
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler("DefaultScheduler");

		log.info("------- Initialization Complete -----------");

		// computer a time that is on the next round minute
		Date runTime = evenMinuteDate(new Date());

		log.info("------- Scheduling Job  -------------------");
		Work work = WorkBuilder.newWork(MyTask.class).withIdentity("job1", "group1").build();
		Work worker = WorkBuilder.newWork(MyTask.class).withIdentity("job1", "group1").build();
		work.setWorkId("hello");
		// define the job and tie it to our HelloJob class
		//JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

		// Trigger the job to run on the next round minute
		Trigger trigger = TriggerBuilder.newTrigger(TriggerType.ALWAYS.getName()).withIdentity("trigger1", "group1").startAt(runTime).build();
		log.info("trigger.getClass().getName() : " + trigger.getClass().getName());
		// Tell quartz to schedule the job using our trigger
		sched.scheduler(worker, trigger);
		log.info(worker.getKey() + " will run at: " + runTime);

		// Start up the scheduler (nothing can actually run until the 
		// scheduler has been started)
		//sched.startAll();

		log.info("------- Started Scheduler -----------------");

		// wait long enough so that the scheduler as an opportunity to 
		// run the job!
		log.info("------- Waiting 65 seconds... -------------");
		try {
			// wait 65 seconds to show job
			//Thread.sleep(65L * 1000L); 
			// executing...
		} catch (Exception e) {
		}

		// shut down the scheduler
		log.info("------- Shutting Down ---------------------");
		//sched.shutdown(true);
		log.info("------- Shutdown Complete -----------------");
	}

	private Date evenMinuteDate(Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.setLenient(true);
		c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	public static void main(String[] args) throws Exception {
		SimpleExample example = new SimpleExample();
		example.run();
	}
}
